package com.gruopbuying.agent.service.impl;

import com.gruopbuying.agent.dao.MenuDao;
import com.gruopbuying.agent.domain.MenuDO;
import com.gruopbuying.agent.dto.RouterDTO;
import com.gruopbuying.agent.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuDao menuMapper;

    @Override
    @CacheEvict(value = "permission", key = "#userId")
    public boolean clearCache(Long userId) {
        return true;
    }

    @Override
    public Object PermsByUserId(Long userId) {
        List<String> permsList = new ArrayList<>();
        List<MenuDO> menuDOs = userMenus(userId);
        for (MenuDO menuDO:menuDOs){
            if(menuDO.getPerms()!=null && ""!=menuDO.getPerms()){
                permsList.add(menuDO.getPerms());
            }
        }
        return permsList;
    }



    public List<MenuDO> userMenus(Long userId) {
        return menuMapper.listMenuByUserId(userId);
    }

    @Override
    public Object RouterDTOsByUserId(Long userId) {
        List<MenuDO> menuDOs = userMenus(userId);
        List<RouterDTO> routerDTOs = new ArrayList<>();
        for (MenuDO menuDO : menuDOs) {
            RouterDTO routerDTO = new RouterDTO();
            routerDTO.setId(menuDO.getMenuId().toString());
            routerDTO.setParentId(menuDO.getParentId().toString());
            routerDTO.setPath(menuDO.getUrl());
            routerDTO.setComponent(menuDO.getComponent());
            routerDTO.setName(menuDO.getName());
            routerDTO.setIconCls(menuDO.getIcon());
            routerDTO.setMenuShow(true);
            routerDTO.setChildren(new ArrayList<>());
            routerDTO.setLeaf(null!=menuDO.getType()&&menuDO.getType()==1);
            routerDTOs.add(routerDTO);
        }
        return RouterDTO.buildList(routerDTOs, 0L);
    }



}
